package org.forever.framework.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.forever.framework.system.entities.SysUser;
import org.forever.framework.system.service.SysUserService;
import org.springframework.orm.hibernate3.HibernateOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class SysUserServiceImpl implements SysUserService {
	@Resource private HibernateOperations hibernateTemplate;

	public int createUser(SysUser... sysUsers) throws Exception {
		if(sysUsers==null)return -1;
		for (SysUser sysUser : sysUsers) {
			hibernateTemplate.save(sysUser);
		}
		return sysUsers.length;
	}

	public SysUser findByUserName(String username) throws Exception {
		List<?> list = hibernateTemplate.find("from SysUser where username=?", username);
		if(list.size()==1)return (SysUser) list.get(0);
		return null;
	}

	public int deleteUserByUserName(String username) throws Exception {
		return hibernateTemplate.bulkUpdate("delete SysUser where username=?",username);
	}

	public int updateUser(SysUser sysUser) throws Exception {
		if(sysUser.getId() == null)return 0;
		if(findById(sysUser.getId())==null)return 0;
		hibernateTemplate.merge(sysUser);
		return 1;
	}

	public SysUser findById(String id) throws Exception {
		List<?> list = hibernateTemplate.find("from SysUser where id=?", id);
		if(list.size()==1)return (SysUser) list.get(0);
		return null;
	}
	
}
